package data_structure;

/**
 * @author 80002165 @date 2017年7月12日 下午6:32:10
 */
public class Map {
    static Entry[] table = new Entry[2] ;
    static class Entry{
        String key ;
        String value ;
        Entry next ;
        @Override
        public int hashCode() {
            final int prime = 31 ;
            int result = 1 ;
            result = prime * result + ((key == null) ? 0 : key.hashCode()) ;
            result = prime * result + ((value == null) ? 0 : value.hashCode()) ;
            return result ;
        }
    }
    
    static int indexFor(int h, int length){
        return h & (length-1) ;
    }
    
    static void transfer(Entry[] newTable){
        Entry[] src = table ;
        for(int i=0; i<src.length; i++){
            Entry e = src[i] ;
            if(e != null){
                src[i] = null ;
                do{
                    Entry next = e.next ;
                    int index = indexFor(e.hashCode(), newTable.length) ;
                    e.next = newTable[index] ;
                    newTable[index] = e ;
                    
                    e = next ;
                    
                }while(e != null) ;
            }
        }
    }
    
    public static void main(String[] args) {
        for(int i = 1 ; i<10 ;i++){
            Entry entry = new Entry() ;
            entry.key = "key_"+i ;
            entry.value = "value_"+i ;
            entry.next = table[1] ;
            table[1] = entry ;
        }
        
        Entry[] newTable = new Entry[3] ;
        
        transfer(newTable);
        
        table = newTable ;
        
        for(int i=0; i<table.length; i++){
            if(table[i] != null){
                for(Entry e = table[i]; e != null ; e = e.next){
                    System.out.println(e.key) ;
                }
            }
        }
        
        
    }
}
